package com.oilsection.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oilsection.demo.controller.req.ZrEventImportReq;
import com.oilsection.demo.controller.req.ZrEventPageQueryReq;
import com.oilsection.demo.controller.req.ZrEventUpdateReq;
import com.oilsection.demo.controller.res.ZrEventPageQueryResponse;
import com.oilsection.demo.mapper.ZrEventMapper;
import com.oilsection.demo.mapper.ZrdMapper;
import com.oilsection.demo.po.ZrEvent;
import com.oilsection.demo.po.Zrd;
import com.oilsection.demo.service.ZrEventService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ZrEventServiceImpl extends ServiceImpl<ZrEventMapper, ZrEvent> implements ZrEventService {
    private final ZrdMapper zrdMapper;
    private final ZrEventMapper zrEventMapper;

    @Override
    public ZrEventPageQueryResponse getZrEventPage(ZrEventPageQueryReq req) {
        List<Long> zrdIds = zrdMapper.selectList(new LambdaQueryWrapper<Zrd>().eq(ObjUtil.isNotEmpty(req.getKqName()), Zrd::getKqName, req.getKqName()).eq(ObjUtil.isNotEmpty(req.getZrdName()), Zrd::getName, req.getZrdName())).stream().map(Zrd::getId).collect(Collectors.toList());
        if (ObjUtil.isEmpty(zrdIds)) {
            return ZrEventPageQueryResponse.builder().len(0).zrEventList(new ArrayList<>()).build();
        }

        List<ZrEventPageQueryResponse.ZrEventQueryDto> zrEventList = new ArrayList<>();

        Page<ZrEvent> pageRes = page(new Page<>(req.getPage(), req.getPageSize()), new LambdaQueryWrapper<ZrEvent>().in(ObjUtil.isNotEmpty(zrdIds), ZrEvent::getZrdId, zrdIds).eq(ObjUtil.isNotEmpty(req.getZrTime()), ZrEvent::getZrTime, req.getZrTime()).orderByDesc(ZrEvent::getZrTime));

        pageRes.getRecords().forEach(zrEvent -> {
            ZrEventPageQueryResponse.ZrEventQueryDto zrEventQueryDto = new ZrEventPageQueryResponse.ZrEventQueryDto();
            Zrd zrd = zrdMapper.selectById(zrEvent.getZrdId());

            zrEventQueryDto.setId(zrEvent.getId());
            zrEventQueryDto.setKqName(zrd.getKqName());
            zrEventQueryDto.setZrdName(zrd.getName());
            zrEventQueryDto.setLocation(zrd.getLocation());
            zrEventQueryDto.setQuantity(zrEvent.getQuantity());
            zrEventQueryDto.setZrTime(zrEvent.getZrTime());
            zrEventList.add(zrEventQueryDto);
        });

        return new ZrEventPageQueryResponse(pageRes.getTotal(), zrEventList);
    }

    @Override
    public void importZrEvent(List<ZrEventImportReq> reqs) {
        reqs.forEach(req -> {
            Zrd zrd = zrdMapper.selectOne(new LambdaQueryWrapper<Zrd>().eq(Zrd::getKqName, req.getKqName())
                    .eq(Zrd::getName, req.getZrdName())
                    .eq(Zrd::getXPoint, req.getXPoint())
                    .eq(Zrd::getYPoint, req.getYPoint())
                    .last("limit 1"));

            if (ObjUtil.isEmpty(zrd)) {
                zrd = new Zrd();
                zrd.setKqName(req.getKqName());
                zrd.setName(req.getZrdName());
                zrd.setXPoint(req.getXPoint());
                zrd.setYPoint(req.getYPoint());
                zrdMapper.insert(zrd);
            }else {
                zrd.setKqName(req.getKqName());
                zrd.setName(req.getZrdName());
                zrd.setXPoint(req.getXPoint());
                zrd.setYPoint(req.getYPoint());
                zrdMapper.updateById(zrd);
            }

            ZrEvent zrEvent = new ZrEvent();
            zrEvent.setQuantity(req.getQuantity());
            zrEvent.setZrTime(req.getZrTime());
            zrEvent.setZrdId(zrd.getId());
            zrEventMapper.insert(zrEvent);
        });
    }

    @Override
    public void deleteZrEventById(Long id) {
        removeById(id);
    }

    @Override
    public void updateZrEventById(ZrEventUpdateReq req) {
        updateById(BeanUtil.copyProperties(req, ZrEvent.class));
    }
}
